//
//  SportViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "SportViewController.h"
#import "SportCollectionViewCell.h"
#import "SportDetailViewController.h"
#import "SportModel.h"
@interface SportViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *sportArray;
@end

@implementation SportViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.sportArray = [NSMutableArray array];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - kP6Height(50)) collectionViewLayout:layout];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.showsHorizontalScrollIndicator =  NO;
    [self.view addSubview: self.collectionView];
    self.navigationController.navigationBar.translucent = YES;
  
    NSMutableArray *subArray = [self witchOne:self.tag];
    
    [self.sportArray addObject:subArray];
    
    
    
    UIImageView  *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, kHeight - kP6Height(40), kWidth, kP6Height(40))];
    imageView.image = [UIImage imageNamed:@"article_special_title_bg"];
    imageView.userInteractionEnabled = YES;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, kP6Height(40), kP6Height(40));
    button.center = imageView.center;
    [button setBackgroundImage:[UIImage imageNamed:@"weibo_backBlack@2x"] forState:UIControlStateNormal];
    [button addTarget: self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:imageView];
    [self.view addSubview:button];
    
}

- (void)buttonAction:(UIButton *)sender{
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
       SportCollectionViewCell *cell = [[self.collectionView visibleCells] firstObject];
    NSIndexPath *path =[ self.collectionView indexPathForCell:cell];
    int x;
    switch (self.tag) {
        case 100:
            x = 16;
            break;
          case 101:
            x = 5;
            break;
        default:
            x = 5;
            break;
    }
    
    if ((path.row + 1) % x == 0 && path.section+ 1 == self.sportArray.count) {
        NSMutableArray *arr = self.sportArray[path.section];
        SportModel  *model = [arr[path.row] firstObject];
     NSMutableArray  *array = [ACHCommon spotrModelAsynchronousURL:model.next_url collectionView:self.collectionView];
       
       [self.sportArray addObject:array];
        
        
    }

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.sportArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    
    return [self.sportArray[section] count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"item";
    [collectionView registerClass:[SportCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    SportCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lonelyImageViewAction:)];
    [cell.lonelyImageView addGestureRecognizer:tap1];
    
    [cell  setModel:[self.sportArray[indexPath.section] objectAtIndex:indexPath.row]];
    [cell addTarget:self action:@selector(sportView:)];
    
    
    
    return cell;
    
}



- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return CGSizeMake(kWidth, kHeight - kP6Height(50));
}

- (void)sportView:(UIGestureRecognizer *)sender{
    
    
    SportPieceView *view = (SportPieceView *)sender.view;
    SportDetailViewController *detail = [[SportDetailViewController alloc]init];
    detail.full_url = view.full_url;
    detail.title1 = view.titleLabel.text;
    detail.auther = view.autherLabel.text;
    [self presentViewController:detail animated:NO completion:nil];
    
    
    
}


- (void)lonelyImageViewAction:(UIGestureRecognizer*)sender{
    
    SportDetailViewController *detail = [[SportDetailViewController alloc]init];
    
    UIImageView *imageView = (UIImageView *)sender.view;
    
    NSIndexPath *path = [self.collectionView   indexPathForCell:[[self.collectionView visibleCells] firstObject] ] ;
    
    NSArray *myArr = [self.sportArray[path.section] objectAtIndex:path.row];
    
    SportModel *model = myArr[imageView.tag - 200];
    detail.full_url = model.full_url;
    detail.title1 = model.title;
    detail.auther = model.auther_name;
    
     [self presentViewController:detail animated:NO completion:nil];
    
    
}



- (void)viewWillDisappear:(BOOL)animated{
     
    
}

- (NSMutableArray *)witchOne:(NSInteger )tag{
    
    switch (tag) {
        case 100:
        return [ACHCommon spotrModelAsynchronousURL:kTodayHot collectionView:self.collectionView];
            break;
         case 101:
          return [ACHCommon spotrModelAsynchronousURL:kSportURL collectionView:self.collectionView];
            break;
            case 102:
            return  [ACHCommon spotrModelAsynchronousURL:kCarBossURL collectionView:self.collectionView];
            break;
        default:
            return [ACHCommon spotrModelAsynchronousURL:kUseCarURL collectionView:self.collectionView];
            break;
    }
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
